
import { Component, OnInit, Input, Injector, Type, TemplateRef, ViewChild } from '@angular/core';
import { NzMessageService, NzNotificationService } from 'ng-zorro-antd';
import { DomSanitizer } from '@angular/platform-browser';
import { NgZorroPageBaseComponent } from '../../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component';
import {
  PagedResultDto, ClassInfoOutput, PagerSearchInput, HttpService, BaseComponentConfig,
  CustomNavigation, KeyValueOutput, SearchValue
} from '../../..';
import { Router, Route, ActivatedRoute } from "@angular/router";
import { map } from 'rxjs/operators';
import { MyeditorComponent } from '../../myeditor/myeditor.component';
import { Observable, Observer } from 'rxjs/';
import { async } from '@angular/core/testing';
import { AuthService } from '../../../core/service/auth.service';
import { isNumber } from 'util';
@Component({
  selector: 'app-grill-data',
  templateUrl: './grill-data.component.html',
  styleUrls: ['./grill-data.component.scss']
})
export class GrillDataComponent extends NgZorroPageBaseComponent implements OnInit {
  fileList: Array<any> = [];
  serverFiles = [];
  uploading = false;
  loading = false;
  constructor(
    injector: Injector,
    public auth: AuthService,
    public sanitizer: DomSanitizer,
    public route: ActivatedRoute
  ) {
    super(injector,
      new BaseComponentConfig(
        "",
        "",
        "",
        "",
        "",
        "",
        "",
        "",
      ));
  }
  ngOnInit() {
    this.route.queryParamMap
      .pipe(map(params => params.get('id') || null))
      .subscribe(e => {
        if (!this._pKeyFieldValue) {
          this._pKeyFieldValue = Number(e);
          if (!this._pKeyFieldValue) {
            this.message.error('无法确定主键信息!');
          } else {
            this.isSpinning = false;
          }
        }

        this.IniObserver.subscribe(sb => {
          this.getFileList();
        });
      });
  }

  /**
  * 图片大小限制检查
  */
  beforeImgUpload = (file: File) => {
    const isLt2M = file.size / 1024 / 1024 < 6;
    if (!isLt2M) {
      this.message.error('文件大小超过6MB!');
    }
    return false;
  }

  /**
   * 用于附件绑定选择文件后的处理函数
   */
  bindingForm = (value) => {

    // this.getBase64(value.file, (img: any) => {
    //   console.log(img);
    //   this.fileList.push(img);
    // });
    // return Observable.create(function (observer: Observer<any>) { }).subscribe();
  }

  handleChange(info: any): void {
    this.fileList = info.fileList.filter(item => {
      item.status = 'done';
      return true;
    });
  }

  async handleUpload() {
    this.uploading = true;
    try {
      const httpBody = {
        id: this._pKeyFieldValue,
        UploadFiles: []
      };
      this.fileList.forEach(e => {
        httpBody.UploadFiles.push({
          FileName: e.name,
          Base64Content: e.thumbUrl
        });
      });
      await this.httpService.post<any>('DrillSubject/UploadFiles', httpBody);
      this.fileList = [];
      this.getFileList();
    } finally {
      this.uploading = false;
    }

  }

  async getFileList() {
    this.serverFiles = await this.httpService.get<any>(`DrillSubject/GetSubjectFiles?id=${this._pKeyFieldValue}`);
  }
  /**
   * 删除文件
   * @param id 删除文件ID
   */
  async deleteFile(id: number) {
    this.nzModalService.confirm({
      nzTitle: '<i>警告</i>',
      nzContent: '<b>是否要删除文件</b>',
      nzOnOk: async () => {
        await this.httpService.get<any>(`DrillSubject/DeleteSubjectFile?subjectId=${this._pKeyFieldValue}&fileId=${id}`);
        this.getFileList();
      }
    });

  }
}
